/*
 * Texture.cpp
 *
 * Copyright (c) 2009-2011 [BRN] aka. SWW13.
 *
 * This file is part of [BRN]Lib.
 *
 * [BRN]Lib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * [BRN]Lib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with [BRN]Lib.  If not, see <http ://www.gnu.org/licenses/>.
 */

#if !defined BRNlib_NoD3D
#include "Texture.h"

namespace BRNlib
{
	namespace D3D
	{
		namespace Core
		{
			Texture::Texture(D3DXDevice *pD3Ddev, DWORD Color)
			{
				GenerateTexture(pD3Ddev, &pTexture, Color);
			}
			D3DXTexture *Texture::GetTexture()
			{
				return pTexture;
			}

			HRESULT Texture::GenerateTexture(D3DXDevice *pD3Ddev, D3DXTexture **ppD3Dtex, DWORD Color32)
			{
				if( FAILED(pD3Ddev->D3DX_CreateTexture(8, 8, 1, 0, D3DFMT_A4R4G4B4, D3DPOOL_MANAGED, ppD3Dtex)) )
					return E_FAIL;
			    
				WORD colour16 =    ((WORD)((Color32>>28)&0xF)<<12)
						|(WORD)(((Color32>>20)&0xF)<<8)
						|(WORD)(((Color32>>12)&0xF)<<4)
						|(WORD)(((Color32>>4)&0xF)<<0);

				D3DLOCKED_RECT d3dlr;    
				(*ppD3Dtex)->LockRect(0, &d3dlr, 0, 0);
				WORD *pDst16 = (WORD*)d3dlr.pBits;

				for(int xy=0; xy < 8*8; xy++)
					*pDst16++ = colour16;

				(*ppD3Dtex)->UnlockRect(0);

				return S_OK;
			}
		}
	}
}

#endif